package com.template.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.template.bean.UserTest;
import com.template.service.UserTestService;
@Service
@Transactional
public class UserTestDao implements UserTestService{
	@Resource
	SessionFactory sessionFactory;

	@Override
	public void save(UserTest userTest) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().persist(userTest);
	}

	@Override
	public void update(UserTest userTest) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().merge(userTest);
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public UserTest find(String userName) {
		// TODO Auto-generated method stub
		return (UserTest)sessionFactory.getCurrentSession().get(UserTest.class, userName);
	}

	@Override
	public void delete(String... userNames) {
		// TODO Auto-generated method stub
		for(String userName:userNames){
			sessionFactory.getCurrentSession().delete(sessionFactory.getCurrentSession().load(UserTest.class, userName));
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public List<UserTest> list() {
		// TODO Auto-generated method stub
		return sessionFactory.getCurrentSession().createQuery("from UserTest").list();
	}

}
